package com.tjshop.android.view;

import java.util.ArrayList;
import java.util.List;

import com.tjshop.android.R;
import com.tjshop.android.model.CouponHomeData;
import com.tjshop.android.model.keyPair;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.PaintDrawable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class CategoryPopWindow extends PopupWindow {
	private Context mContext;
	private CouponHomeData homeData;
	private OnClickListener clickListener;
	private View contentView;
	private ListView left, right;

	public CategoryPopWindow(Context context, final List<keyPair> leftData,
			final List<List<keyPair>> rightData, OnClickListener listener) {
		this.mContext = context;
		this.homeData = homeData;
		this.clickListener = listener;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		contentView = inflater.inflate(R.layout.popupwindow_for_category, null);
		// 设置SelectPicPopupWindow的View
		this.setContentView(contentView);
		setFocusable(true);
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		int width = wm.getDefaultDisplay().getWidth();
		int height = wm.getDefaultDisplay().getHeight();
		this.setWidth(width);
		this.setHeight(height / 3);

		// this.setFocusable(true);
		this.setOutsideTouchable(true);
		this.setBackgroundDrawable(new PaintDrawable());
		this.update();
		// setAnimationStyle(R.style.AnimationFade);

		left = (ListView) contentView.findViewById(R.id.category_left);
		right = (ListView) contentView.findViewById(R.id.category_right);

		List<String> lData = new ArrayList<String>();
		for (keyPair kp : leftData) {
			lData.add(kp.getName());
		}
		final MyAdapter leftAdapter = new MyAdapter(context, lData);
		leftAdapter.setSelectItem(0);
		left.setAdapter(leftAdapter);
		left.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					final int position, long arg3) {
				leftAdapter.setSelectItem(position);
				final List<String> rData = new ArrayList<String>();
				for (keyPair kp : rightData.get(position)) {
					rData.add(kp.getName());
				}
				if (rData.size() == 0) {
					clickListener.onClick(leftData.get(position).getId(), "0");
					CategoryPopWindow.this.dismiss();
					return;
				}
				right.setAdapter(new MyAdapter(mContext, rData));
				right.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						if (clickListener != null) {
							clickListener.onClick(leftData.get(position)
									.getId(), rightData.get(position).get(arg2)
									.getId());
							CategoryPopWindow.this.dismiss();
						}
					}
				});
			}
		});
	}

	public static interface OnClickListener {
		public void onClick(String top_kind, String sub_kind);
	}

	/**
	 * 显示popupWindow
	 * 
	 * @param parent
	 */
	@SuppressLint("NewApi")
	public void showPopupWindow(View parent) {
		if (!this.isShowing()) {
			int[] location = new int[2];
			parent.getLocationOnScreen(location);
			this.showAtLocation(parent, Gravity.NO_GRAVITY, location[0],
					location[1] + parent.getHeight());
		} else {
			this.dismiss();
		}
	}

	public final class ViewHolder {
		public TextView titleText;
	}

	public class MyAdapter extends BaseAdapter {
		private LayoutInflater mInflater;
		private List<String> mData;

		public MyAdapter(Context context, List<String> mData) {
			this.mInflater = LayoutInflater.from(context);
			this.mData = mData;
		}

		public int getCount() {
			return mData.size();
		}

		public Object getItem(int arg0) {
			return mData.get(arg0);
		}

		public long getItemId(int arg0) {
			return arg0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(
						android.R.layout.simple_list_item_1, null);
				holder.titleText = (TextView) convertView
						.findViewById(android.R.id.text1);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.titleText.setText((String) mData.get(position));

			if (position == selectItem) {
				convertView.setBackgroundColor(0xff808080);
			} else {
				convertView.setBackgroundColor(Color.TRANSPARENT);
			}
			return convertView;
		}

		public void setSelectItem(int selectItem) {
			this.selectItem = selectItem;
			this.notifyDataSetInvalidated();
		}

		private int selectItem = -1;
	}

}
